package com.shisfish.library.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	/**
     * 正则表达式中“\d”表示[0-9]的数字，
     * “\d+”表示由[0-9]的数字组成的数字，
     * “\w”表示[A-Z0-9]，
     * “\w+”表示由数字、26 个英文字母或者下划线组成的字符串，
     * “\d+.+\d+”表示小数
     */
    private static final String REGULAR_WORD_NUMBER = "\\w+";
    
    private static final String REGULAR_CHINESE = "[\u4e00-\u9fa5]";
	
	public static String null2Empty(String str) {
		if (str == null) {
			str = "";
		}
		return str;
	}
	
	public static void main(String[] args) {
		String str = null2Empty(null);
		System.out.println(str + "___ppp");
		if (str.isEmpty()) {
			System.out.println("1");
		}
		else {
			System.out.println("2");
		}
		
		String choose = "SUN公司被Oracle收购，是否意味着java111被逼上了111死路？";
		List<String> wordAndNumber = getWordAndNumber(choose);
		List<String> singleChinese =  getSingleChinese(choose);
		
		for (int i = 0; i < wordAndNumber.size(); i++) {
			System.out.println(wordAndNumber.get(i));
		}
		for (int i = 0; i < singleChinese.size(); i++) {
			System.out.println(singleChinese.get(i));
		}
	}
	
	public static List<String> getWordAndNumber(String str) {
		List<String> words = new ArrayList<String>();
		
		Pattern pattern = Pattern.compile(REGULAR_WORD_NUMBER);  
        Matcher matcher = pattern.matcher(str);
		
        while (matcher.find()) {
			words.add(matcher.group());
		}
        
		return words;
	}
	
	public static List<String> getSingleChinese(String str) {
        Pattern pattern = Pattern.compile(REGULAR_CHINESE);
        Matcher matcher = pattern.matcher(str);
        List<String> singleCh = new ArrayList<String>();
        
        while (matcher.find()) {
        	singleCh.add(matcher.group());
		}
        
        return singleCh;
    }
	
	public static List<String> getSingleString(String str) {
		
        Pattern pattern=Pattern.compile(REGULAR_CHINESE);  

        List<String> list = new ArrayList<String>();
        
        String words = "";
        
        for (int i = 0; i < str.length(); i++) {
        	if ((str.charAt(i)+"").getBytes().length == 1) {
        		words += str.charAt(i);
			}
        	else if ((str.charAt(i)+"").getBytes().length == 2 || (str.charAt(i)+"").getBytes().length == 3) {
        		
        		if (!"".equals(words)) {
        			list.add(words);
				}
        		words = "";
        		Matcher matcher=pattern.matcher(str.charAt(i)+""); 
        		
        		if (matcher.find()) {
					list.add(matcher.group());
				}
			}
        }
        
        list.add(words);
        
        return list;
	}
	
	
	
}
